package com.huas.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huas.common.PageResult;
import com.huas.constant.MessageConstant;
import com.huas.entity.Article;
import com.huas.entity.LoginUser;
import com.huas.entity.User;
import com.huas.exception.BaseException;
import com.huas.mapper.ArticleMapper;
import com.huas.mapper.UserMapper;
import com.huas.service.ArticleService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    ArticleMapper articleMapper;
    @Resource
    UserMapper userMapper;
    /**
     * 条件分页列表查询
     */
    @Override
    @Transactional
    public PageResult page(Integer page, Integer pageSize, String title,String category) {
        //PageHelper设置分页参数
        PageHelper.startPage(page,pageSize);
        /**
         * 底层实现原理：ThreadLocal
         * startPage:将page,pageSize都存入本地空间，调用mapper的方法又取出进行动态拼接
         */
        //List<Member> MemberList = MemberMapper.page(name,gender);
        //获取的其实是一个ArrayList集合
        Page<Article> p = articleMapper.page(title,category);

        List<Article> articleList = p.getResult();
        //遍历集合得到每一个实体类对象
        for (Article article : articleList) {
            //得到发布人id查询用户得到对应name名称
            Integer authorId = article.getAuthorId();
            User user = userMapper.findById(authorId);
            if(user != null){
                article.setAuthor(user.getName());
            }
        }
        return new PageResult(p.getTotal(),p.getResult());
    }
    /**
     * 添加信息
     */
    @Override
    @Transactional
    public void insert(Article article) {
        //获取当前用户的用户id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Integer id = loginUser.getUser().getId();
        article.setAuthorId(id);
        article.setCreateTime(LocalDateTime.now());
        articleMapper.reset();
        articleMapper.insert(article);
    }
    /**
     * 修改信息
     */
    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }
    /**
     * 删除信息
     */
    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }
    /**
     * 批量删除信息
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        if(ids.isEmpty()){
            throw new BaseException("未选定删除信息");
        }
        articleMapper.deleteBatch(ids);
    }
    /**
     * 查询文章分类
     */
    @Override
    public List<String> selectArticleCategory() {
        return articleMapper.selectArticleCategory();
    }

    /**
     * 用户查询文章集合
     * @return
     */
    @Override
    public List<Article> selectArticleList() {
        List<Article> articleList =articleMapper.selectArticleList();
        //遍历集合得到每一个实体类对象
        for (Article article : articleList) {
            //得到发布人id查询用户得到对应name名称
            Integer authorId = article.getAuthorId();
            User user = userMapper.findById(authorId);
            if(user != null){
                article.setAuthor(user.getName());
            }else {
                throw new BaseException(MessageConstant.AUTHOR_FAILED);
            }
        }
        return articleList;
    }

    @Override
    public Article selectArticle(Integer id) {
        Article article = articleMapper.selectArticle(id);
        //得到发布人id查询用户得到对应name名称
        Integer authorId = article.getAuthorId();
        User user = userMapper.findById(authorId);
        if(user != null){
            article.setAuthor(user.getName());
        }else {
            throw new BaseException(MessageConstant.AUTHOR_FAILED);
        }
        return article;
    }
}
